//
//  NSDate+JwCate.m
//  JwPack
//
//  Created by 陈警卫 on 2023/2/23.
//

#import "NSDate+JwCate.h"

@implementation NSDate (JwCate)

/// 年
- (NSInteger)jw_year {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:self] year];
}

/// 月
- (NSInteger)jw_month {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:self] month];
}

/// 日
- (NSInteger)jw_day {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:self] day];
}

/// 时
- (NSInteger)jw_hour {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:self] hour];
}

/// 分
- (NSInteger)jw_minute {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitMinute fromDate:self] minute];
}

/// 秒
- (NSInteger)jw_second {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self] second];
}

/// 毫秒
- (NSInteger)jw_nanosecond {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self] nanosecond];
}

/// 星期(周日:1 周一:2 ...)
- (NSInteger)jw_weekday {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:self] weekday];
}

/// 周
- (NSInteger)jw_weekdayOrdinal {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekdayOrdinal fromDate:self] weekdayOrdinal];
}

/// 本月的第几周
- (NSInteger)jw_weekOfMonth {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekOfMonth fromDate:self] weekOfMonth];
}

/// 本年的第几周
- (NSInteger)jw_weekOfYear {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekOfYear fromDate:self] weekOfYear];
}

/// 季度
- (NSInteger)jw_quarter {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitQuarter fromDate:self] quarter];
}

/// 闰月
- (BOOL)jw_isLeapMonth {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitQuarter fromDate:self] isLeapMonth];
}

/// 闰年
- (BOOL)jw_isLeapYear {
    NSUInteger year = self.jw_year;
    return ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0)));
}

/// 是否今天
- (BOOL)jw_isToday {
    if (fabs(self.timeIntervalSinceNow) >= 60 * 60 * 24) return NO;
    return [NSDate new].jw_day == self.jw_day;
}

/// 加年
- (NSDate *)jw_dateAddYears:(NSInteger)years {
    NSCalendar *calendar =  [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setYear:years];
    return [calendar dateByAddingComponents:components toDate:self options:0];
}

/// 加月
- (NSDate *)jw_dateAddMonths:(NSInteger)months {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setMonth:months];
    return [calendar dateByAddingComponents:components toDate:self options:0];
}

/// 加周
- (NSDate *)jw_dateAddWeeks:(NSInteger)weeks {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setWeekOfYear:weeks];
    return [calendar dateByAddingComponents:components toDate:self options:0];
}

/// 加天
- (NSDate *)jw_dateAddDays:(NSInteger)days {
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 86400 * days;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;
}

/// 加时
- (NSDate *)jw_dateAddHours:(NSInteger)hours {
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 3600 * hours;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;
}

/// 加分
- (NSDate *)jw_dateAddMinutes:(NSInteger)minutes {
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + 60 * minutes;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;
}

/// 加秒
- (NSDate *)jw_dateAddSeconds:(NSInteger)seconds {
    NSTimeInterval aTimeInterval = [self timeIntervalSinceReferenceDate] + seconds;
    NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
    return newDate;
}

/// 时间戳转date
+ (NSDate *)jw_dateWithStamp:(NSInteger)stamp {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:stamp];
    return date;
}

/// 时间戳格式化
/// format默认 "yyyy-MM-dd HH:mm:ss"
+ (NSString *)jw_stringWithStamp:(NSInteger)stamp {
    NSString *dateString = [NSDate jw_stringWithStamp:stamp format:nil];
    return dateString;
}

/// 时间戳格式化
/// format默认 "yyyy-MM-dd HH:mm:ss"
+ (NSString *)jw_stringWithStamp:(NSInteger)stamp format:(NSString *)format {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:stamp];
    NSString *dateString = [date jw_stringWithFormat:format];
    return dateString;
}

/// 时间格式化
/// format默认 "yyyy-MM-dd HH:mm:ss"
- (NSString *)jw_stringWithFormat:(NSString *)format {
    return [self jw_stringWithFormat:format timeZone:nil locale:nil];
}

/// 时间格式化
/// format默认 "yyyy-MM-dd HH:mm:ss"
- (NSString *)jw_stringWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (format) {
        format = @"yyyy-MM-dd HH:mm:ss";
    }
    [formatter setDateFormat:format];
    if (timeZone) [formatter setTimeZone:timeZone];
    if (locale) [formatter setLocale:locale];
    return [formatter stringFromDate:self];
}

/// 格式化时间转date
/// format默认 "yyyy-MM-dd HH:mm:ss"
+ (NSDate *)jw_dateWithString:(NSString *)dateString format:(NSString *)format {
    return [self jw_dateWithString:dateString format:format timeZone:nil locale:nil];
}

/// 格式化时间转date
/// format默认 "yyyy-MM-dd HH:mm:ss"
+ (NSDate *)jw_dateWithString:(NSString *)dateString format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (format) {
        format = @"yyyy-MM-dd HH:mm:ss";
    }
    [formatter setDateFormat:format];
    if (timeZone) [formatter setTimeZone:timeZone];
    if (locale) [formatter setLocale:locale];
    return [formatter dateFromString:dateString];
}

/// 时间戳
+ (NSString *)jw_timestamp {
    return [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
}

/// 时间戳
+ (NSString *)jw_timestampMilli {
    return [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970] * 1000];
}

/// 时钟戳
+ (NSString *)jw_dateFmtString {
    NSDateFormatter *formatter  = [[NSDateFormatter alloc] init];
    formatter.dateFormat  = @"yyyyMMddHHmmssSSS";
    NSString *datefrom = [formatter stringFromDate:[NSDate date]];
    return datefrom;
}

@end
